#!/usr/bin/env python
# -*- coding: utf-8 -*-

from threading import Thread, Lock
import time

g_num = 0
queue = []

def test1():
    global g_num
    for i in range(1000000):
        if mutex.acquire(False):#False:非阻塞；True：阻塞
            g_num += 1
            mutex.release()
        else:
            queue.append(1)
    print("test1 --> %d"%g_num)

def test2():
    global g_num
    for i in range(1000000):
        if mutex.acquire(False):
            g_num +=1
            mutex.release()
        else:
            queue.append(1)
    print("test2 --> %d"%g_num)

mutex = Lock()
t1 = Thread(target=test1)
t2 = Thread(target=test2)
t1.start()
# time.sleep(2)
t2.start()
t1.join()
t2.join()
print("main --> %s"%g_num)
print(len(queue)+g_num)

